<?php
namespace plugins\finance\controller;

use cmf\controller\PluginAdminBaseController;
use think\Db;
use think\Request;
use app\common\lib\model_operate\Transit;
use plugins\finance\model\FinanceExtractModel;
use plugins\finance\model\FinanceRechargeModel;
use plugins\finance\model\FinanceModel;
use plugins\pay\lib\Pay;
use plugins\order\model\OrderModel;


//财务的审核功能
class AuditController extends PluginAdminBaseController
{

	public function index()
	{

	}

	//提现申请的列表
	public function extract_list()
	{
		$param = $this->request->param();

		//流水记录
		$search =  Transit::createTags('search');
		$search->setParam($param);
		$table = Transit::createTags('table');
		$table->setParam($search);	
		$pagesize = isset( $param [ 'pagesize' ] ) ? $param [ 'pagesize' ] : 20;

		$sear = [];

        //流水记录列表
		$list = $table->query()
		        ->order('id desc')
                ->paginates($pagesize,false,['query'=>$sear]);


		$this->assign('search',$search);
		$this->assign('table',$table);
		$this->assign('list',$list);

		return $this->fetch();
	}

	//同意提现
	public function extract_agree()
	{
		$param = $this->request->post();
		Db::startTrans();
		//所选内容
		$list = ( new FinanceExtractModel )
		->where(['extract_num'=>['in',$param['extract_num']]])
		->select();

		//遍历
		foreach ($list as $key => $value) {

			if($value->status != 1){
				continue;
			}

			//提现操作
			$return = $value->extract();
			//处理日志
			$log = json_decode($value['log'],true);
			//添加通过日志
			$log[] = ['time'=>time(),'message'=>$return['message']];
			//存储日志
			$value->log = json_encode($log);

			if(!$return['status']){
				$value->save();
				return $this->error($value->extract_num.'操作失败');
			}else{
				//3为申请通过
				$value->status = 3;				
				$value->save();
			}
		}

		Db::commit();
		return $this->success('操作成功,选中的待处理审核已通过');
	}

	//拒绝提现
	public function extract_refuse()
	{
		$param = $this->request->param();
		Db::startTrans();
		//所选内容
		$list = ( new FinanceExtractModel )
		->where(['extract_num'=>['in',$param['extract_num']]])
		->select();

		//遍历
		foreach ($list as $key => $value) {

			if($value->status != 1){
				continue;
			}

			//处理日志
			$log = json_decode($value['log'],true);
			//添加拒绝日志
			$log[] = ['time'=>time(),'message'=>'审核拒绝,拒绝原因：'.$param['reason']];
			//存储日志
			$value->log = json_encode($log);

			//2为拒绝
			$value->status = 2;

			$result = $value->save();

			if(!$result){
				return $this->error($value->extract_num.'操作失败');
			}			

		}

		Db::commit();
		return $this->success('操作成功,选中的待处理审核已拒绝');

	}

	//充值退款申请列表
	public function recharge_refund_list()
	{
		$param = $this->request->param();

		//流水记录
		$search =  Transit::createTags('search');
		$search->setParam($param);
		$table = Transit::createTags('table');
		$table->setParam($search);	
		$pagesize = isset( $param [ 'pagesize' ] ) ? $param [ 'pagesize' ] : 20;

		$sear = [];		

		$list = ( new FinanceRechargeModel )
				->where(['status'=>['in',['3']]])
		        ->order('id desc')
                ->paginates($pagesize,false,['query'=>$sear]);

		$this->assign('search',$search);
		$this->assign('table',$table);
		$this->assign('list',$list);

		return $this->fetch();		

	}

	//同意退款
	public function recharge_refund_agree()
	{
		$param = $this->request->param();
		//所选内容
		$list = ( new FinanceRechargeModel )
		->where(['recharge_num'=>['in',$param['recharge_num']]])
		->select();

		//遍历
		foreach ($list as $key => $value) {

			if($value->status != 3){
				continue;
			}

			//得到账户
			$finance  = ( new FinanceModel )->get(['finance_id'=>$value->finance_id,'finance_type'=>'member']);

			if($finance->balance < $value->money){
				return $this->error($value->recharge_num.'操作失败,用户账户余额不足以退款！');
			}

			//调用pay创建退款
			$result = $value->rechagreReturn();
			if(!$result['status']){
				return $this->error($value->recharge_num.'操作失败,'.$result['message']);
			}else{
				//减少账户金额
				$finance->balance = $finance->balance - $value->money;
				$result = $finance->save();
				if(!$result){
					return $this->error($value->recharge_num.'操作失败,金额变动失败');
				}
			}
		}
		return $this->success('操作成功,选中的待退款申请已通过');
	}

	//拒绝退款
	public function recharge_refund_refuse()
	{
		$param = $this->request->param();
		//所选内容
		$list = ( new FinanceRechargeModel )
		->where(['recharge_num'=>['in',$param['recharge_num']]])
		->select();

		foreach ($list as $key => $value) {
			if($value->status != 3){
				continue;
			}

			$value->status = 4;
			$value->save();
		}

		return $this->success('操作成功,选中的待退款申请已拒绝');

	}

	public function test()
	{



	}


}